package com.hs.myshell.action;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.internal.UIPlugin;

import com.hs.myshell.edit.TerminalEditor;
import com.hs.myshell.edit.TerminalEditorInput;
import com.hs.myshell.nls.Messages;
import com.hs.myshell.ssh.ISSHClient;
import com.hs.myshell.ssh.ISSHClientSSHJ;
import com.hs.myshell.ssh.SSHClient;

/**
 * 参考内置的 终端控制器
 * 
 * @author Administrator
 * @see org.eclipse.tm.internal.terminal.emulator.VT100TerminalControl
 * @see org.eclipse.tm.internal.terminal.provisional.api.provider.TerminalConnectorImpl
 
 
 org.eclipse.tm.terminal.view.ui.internal.dialogs.LaunchTerminalSettingsDialog#okPressed 连接菜单
 
 org.eclipse.tm.terminal.view.ui.interfaces.IConfigurationPanel 基础配置框
 org.eclipse.tm.terminal.connector.ssh.controls.SshWizardConfigurationPanel
 
 * @see org.eclipse.tm.terminal.view.ui.internal.dialogs.LaunchTerminalSettingsDialog#createDialogAreaContent 启动对话框
   org.eclipse.tm.terminal.view.ui.internal.dialogs.LaunchTerminalSettingsDialog#okPressed->
   String delegateId = terminalLabel != null ? ((ILauncherDelegate)this.label2delegate.get(terminalLabel)).getId() : null;获取选择的启动类
   org.eclipse.tm.terminal.view.ui.internal.handler.LaunchTerminalCommandHandler#execute ->  delegate.execute(properties, (Done)null); 在Handler里面启动执行器
   org.eclipse.tm.terminal.connector.ssh.launcher.SshLauncherDelegate#execute 实际的启动类
   org.eclipse.tm.terminal.view.ui.services.TerminalService#openConsole *********真正的启动类********
   org.eclipse.tm.terminal.view.ui.manager.ConsoleManager#openConsole 一个连接标签页
   org.eclipse.tm.terminal.view.ui.tabs.TabFolderManager#createTabItem 创建一个标签页
   内部创建了一个ITerminalViewControl(子类org.eclipse.tm.internal.terminal.emulator.VT100TerminalControl)嵌入在 CTabItem里面的
   实际的一个org.eclipse.tm.internal.terminal.emulator.VT100TerminalControl#connectTerminal 模拟器-连接远端
   org.eclipse.tm.internal.terminal.emulator.VT100TerminalControl#waitForConnect 等待ssh远程响应
   通过一个canvas滚动实现的org.eclipse.tm.internal.terminal.textcanvas.PollingTextCanvasModel#startPolling
   org.eclipse.tm.internal.terminal.emulator.VT100TerminalControl#startReaderJob 通过后台任务不断的轮训输入流---------->获取用户输入, 
   org.eclipse.tm.internal.terminal.emulator.VT100Emulator#processText 处理新的变更的文本
   
   org.eclipse.tm.internal.terminal.emulator.VT100Emulator#VT100Emulator 模拟器初始化
   
   ********************************************core代码把inputstream交给一个Reader去读取 用户的输入
   org.eclipse.tm.internal.terminal.emulator.VT100TerminalControl#VT100TerminalControl
 
 * @see org.eclipse.tm.terminal.view.ui.interfaces.ILauncherDelegate 不同终端的启动器
 * 	    org.eclipse.tm.internal.terminal.provisional.api.ITerminalConnector 不同终端的连接器
 *		org.eclipse.tm.terminal.view.ui.manager.ConsoleManager#openConsole(String, String, String, String, org.eclipse.tm.internal.terminal.provisional.api.ITerminalConnector, Object, Map<String,Boolean>)
 * @see org.eclipse.tm.terminal.view.core.interfaces.ITerminalService 终端操作类
 
 ********************************************core代码 读取连接的输出
 org.eclipse.tm.terminal.connector.ssh.connector.SshConnector#connect 是一个Thread的后台线程,不断轮训 shell的输出
 org.eclipse.tm.terminal.connector.ssh.connector.SshConnection#run
 * org.eclipse.tm.terminal.connector.ssh.connector.SshConnection.MyUserInfo#promptYesNo 弹出 统一bluePrint的 confirm框
 * 

 *
 */
public class OpenTerminalAction extends Action implements ISelectionListener, IWorkbenchAction {

	private final IWorkbenchWindow window;
	private IStructuredSelection selection;
	
	
	
	public OpenTerminalAction(IWorkbenchWindow window,IStructuredSelection selection) {
		this.window = window;
		this.selection = selection;
		window.getSelectionService().addSelectionListener(this);
	}

	@Override
	public void dispose() {
		window.getSelectionService().removeSelectionListener(this);
	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection input) {
		if (input instanceof IStructuredSelection) {
			selection = (IStructuredSelection)input;
			setEnabled(selection.size() == 1 && selection.getFirstElement() instanceof Object);
		}else {
			setEnabled(false);
		}

	}

	public void run() {
		Object item = selection.getFirstElement();
		//ҵ�����
		Object dd = (Object)item;
		IWorkbenchPage page = window.getActivePage();
		TerminalEditorInput terminalEditorInput = new TerminalEditorInput("ip");
		try {
			TerminalEditor openEditor = (TerminalEditor)page.openEditor(terminalEditorInput, TerminalEditor.ID);
			System.out.println(openEditor.getTitle());
			
	        long start = System.currentTimeMillis();
	        
//	        ILog log = UIPlugin.getDefault().getLog();
//	        Status status = new Status(IStatus.ERROR, "", IStatus.ERROR, Messages.ssh_host, null);
//	        log.log(status);
	        System.out.println(Messages.ssh_host);
	        ISSHClient sshClient = new ISSHClientSSHJ(Messages.ssh_host, 22, Messages.ssh_user, Messages.ssh_pwd);
	        sshClient.login();
	        long end = System.currentTimeMillis();
	        System.out.println("login: "+ (end-start));
	        start = System.currentTimeMillis();
	        sshClient.shell(openEditor);
	        end = System.currentTimeMillis();
	        System.out.println("shell: "+ (end-start));
	        openEditor.setISSHClient(sshClient);
	        sshClient.sendCmd("pwd/r/n",openEditor);
	        openEditor.showMsg();
	        end = System.currentTimeMillis();
	        System.out.println("showMsg: "+ (end-start));
//	        openEditor.startReaderJob();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}
